package imagecutter;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

/**
 *
 * @author faila
 */
public class ImageOperation {

    BufferedImage img;
    int imageHeight, imageWidht;

    /**
     * Load image file, check exist file and get image height and widht
     * @param configurationImage
     * @throws RuntimeException thrown when problems with image loading
     */
    /*
    readImage
    first check that the *. jpg file exists - if not displays an error.
    load the image using the method loadImageFile ()
    reads the image characteristics (height, width)
     */
    public void readImage(String configurationImage) {

        boolean imageExist = Utils.checkFile(configurationImage);
        if (!imageExist) {
            throw new IllegalArgumentException("File (" + configurationImage + ") doesn't exist or is not a file");
        }

        loadImageFile(configurationImage);
        loadImageProperties();
    }

    /**
     * Load image from file.
     * @param String path
     */
    /*
    loadImageFile
    load the image if it exists, otherwise displays an error.
    Input is a command line argument
     */
    public void loadImageFile(String path) {

        try {
            img = ImageIO.read(new File(path));
        } catch (IOException e) {
            System.out.println("Problem with loading image file.");

        }

    }

    /**
     * Get image properties from file(properties - height, widht).
     * @param String path
     */
    /*
    load a properties of image - width and height
     */
    public void loadImageProperties() {

        imageHeight = img.getHeight();
        imageWidht = img.getWidth();

    }

    /**
     * Create part images to files.
     * @return image type as int
     */
    public int getImageType() {
        return img.getType();
    }

    /**
     * Create part images to files.
     * @param BufferedImage [] imageParts
     */
    /*
    createImageFile create new images cut from the original single image
     */
    public void createImage(BufferedImage[] imageParts) {

        for (int i = 0; i < imageParts.length; i++) {
            try {
                ImageIO.write(imageParts[i], "jpg", new File("img" + i + ".jpg"));
            } catch (Exception e) {

                System.out.println("Image can not be further cut.");
            }

        }
    }
}


